/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 31.07.2005
 *
 *************************************************/
package fightplan.views.tables;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;

import fightplan.business.Result;
import fightplan.ui.BeanPropertyListCellRenderer;

@SuppressWarnings("serial")
public class ResultCellEditor extends DefaultCellEditor {

	public ResultCellEditor() {
		super(createComboBox());
	}

	private static JComboBox createComboBox() {
		JComboBox comboBox = new JComboBox(Result.ALL_VALUES);
		comboBox.setRenderer(new BeanPropertyListCellRenderer("name"));
		comboBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		comboBox.setEditable(false);
		return comboBox;
	}

	public Object getCellEditorValue() {
		// Hack
		SwingUtilities.getWindowAncestor((JComboBox) getComponent()).repaint();

		Object o = ((JComboBox) getComponent()).getSelectedItem();
		if (o instanceof Result) {
			return o;
		} else if (o instanceof String) {
			return Result.parse((String) o);
		}
		return Result.OPEN;
	}

}
